{% comment %} tagcloud(min_font, max_font, amount, color, start_color, end_color) {% endcomment %}
{% capture tagcloud %}
  {% assign tags = site.tags %}
  {% assign max = max_font %}
  {% assign min = min_font %}
  {% assign sizes = '' | split: '' %}
  {% assign unit = 'px' %}
  {% assign start_color = start_color | split: ',' %}
  {% assign end_color = end_color | split: ',' %}

  {% for tag in tags %}
    {% if forloop.index > amount %}
      {% break %}
    {% endif %}
    {% assign sizes = sizes | push: tag[1].size %}
  {% endfor %}

  {% assign sizes = sizes | uniq | sort %}
  {% assign length = sizes.size | minus: 1 %}

  {% for tag in tags %}
    {% if forloop.index > amount %}
      {% break %}
    {% endif %}

    {% assign ratio = 0 %}

    {% if length > 0 %}
      {% for size in sizes %}
        {% if size == tag[1].size %}
          {% assign ratio = forloop.index0 | times: 1.0 | divided_by: length %}
          {% break %}
        {% endif %}
      {% endfor %}
    {% endif %}

    {% assign tmp = max | minus: min | times: ratio %}
    {% assign size = tmp | plus: min %}
    {% assign tmp = size | round: 2 %}

    {% assign style = 'font-size: ' | append: tmp | append: unit | append: ';' %}

    {% if color %}
      {% assign mid_color = '' | split: '' %}
      {% for i in (0..2) %}
        {% assign tmp = end_color[i] | minus: start_color[i] | times: ratio %}
        {% assign tmp = start_color[i] | plus: tmp | round %}
        {% assign mid_color = mid_color | push: tmp %}
      {% endfor %}
      {% assign mid_color = 'rgb(' | append: mid_color[0] | append: ',' | append: mid_color[1] | append: ',' | append: mid_color[2] | append: ')' %}
      {% assign style = style | append: 'color: ' | append: mid_color %}
    {% endif %}
    {% assign tag_url_encode = tag[0] | url_encode | replace: '+', '%20' %}
    <a href="{{ '/tag/#/' | relative_url | append: tag_url_encode }}" style="{{ style }}">{{ tag[0] }}</a>
  {% endfor %}

{% endcapture %}
{% assign tagcloud = tagcloud | normalize_whitespace %}
